package calao.storage;

import java.lang.Comparable;

public class FieldType<V> 
{
	
	public enum Enum {
		STRING, INTEGER, SHORT, LONG, DOUBLE, OBJECT, BINARY 
	}
	
	final Class<V> type;
	final Enum enumValue;
	
	FieldType(Class<V> type, Enum enumValue)
	{
		this.type = type;
		this.enumValue = enumValue;
	}
	
	public final Enum toEnum() {
		return enumValue;
	}
	
	public Object[] newValueArray(int length) {
		return (Object[])java.lang.reflect.Array.newInstance(type, length);
	}
	
	public Class<V> getValueType()
	{
		return type;
	}
	
	@SuppressWarnings("unchecked")
	public int compare(V value1, V value2)
	{
		try {
			return ((Comparable<V>)value1).compareTo(value2);
		}
		catch (ClassCastException e) {
			throw new UnsupportedOperationException();
		}
	}
	
	public boolean supportsCompare()
	{
		return Comparable.class.isAssignableFrom(type);
	}
		
}
